'use strict';
require('./index.css');
/*require('util/wangEditor/index.js');*/
var _mm = require('util/mm.js');
var Pagination = require('util/pagination/index.js');
var Ueditor = require('util/ueditor/ueditor.all.js');
var Datepair = require('util/datepair/index.js');

var _admin = require('service/admin-service.js');

var templateOperation_right = require('./operation-right.string');
var templateOperation_left = require('./operation-left.string');
var templateWorkTrend_right = require('./workTrend-right.string');
var templateWorkTrend_left = require('./workTrend-left.string');
var templateTraining_right = require('./training-right.string');
var templateTraining_left = require('./training-left.string');
var templateInteraction_right = require('./interaction-right.string');
var templateInteraction_left = require('./interaction-left.string');
var templatePublish_right = require('./publish-right.string');
var templatePublish_left = require('./publish-left.string');
var templateTeacherlib_right = require('./teacherlib-right.string');
var templateTeacherlib_left = require('./teacherlib-left.string');
var templateStatement_right = require('./statementlib-right.string');
var templateStatement_left = require('./statementlib-left.string');
var templateSetting_right = require('./setting-right.string');
var templateSetting_left = require('./setting-left.string');

var templateTrainingInform_right = require('./two/trainingInform-left.string');


var page = {
    //封装分页数据
    data: {
        listParam: {
            wtId: '',
            title: '',
            soureCrmpy: '',
            orderBy: 'default',
            pageNum: 1,
            pageSize: 8,
            num:''
        }

    },
    init: function() {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function() {
        /*Ueditor.UE.getEditor('container');*/
        // initialize input widgets first

        $('#basicExample .time').timepicker({

            'showDuration': true,

            'timeFormat': 'g:ia'

        });

        $('#basicExample .date').datepicker({

            'format': 'm/d/yyyy',

            'autoclose': true

        });

        // initialize datepair

        $('#basicExample').datepair();

        //加载用户信息

    },
    bindEvent: function() {

        var _this = this;
        //工作动态搜索功能
        $(document).on('click', '#search', function() {
            _this.data.listParam.title = $.trim($('#title').val());
            _this.data.listParam.soureCrmpy = $.trim($('#soureCrmpy').val());
            _this.loadWorkTrend();
            _this.ShowLeftSubMenu('001');
        });

        //top工作动态按钮点击  内部加载左右信息  以及默认打开左侧第一个窗口
        //工作台
        $("#topa_1").click(function() {
            _this.loadOperation();
            _this.ShowLeftSubMenu('001');
            _admin.searchTraining('',function(res){
                $('#num1').html(res.list.length);
           },function(errMsg){
           });
        });
        //工作动态
        $("#topa_2").click(function() {
            _this.loadWorkTrend();
            _this.ShowLeftSubMenu('001');
        });
        //培训通知
        $("#topa_3").click(function() {
            _this.loadTraining(1);
            _this.ShowLeftSubMenu('001');
        });
        //互动交流
        $("#topa_4").click(function() {
            _this.loadInteraction();
            _this.ShowLeftSubMenu('001');
        });
        //信息发布
        $("#topa_5").click(function() {
            _this.loadPublish();
            _this.ShowLeftSubMenu('001');
        });
        //师资库
        $("#topa_6").click(function() {
            _this.loadTeacherlib();
            _this.ShowLeftSubMenu('001');
        });
        //报表统计
        $("#topa_7").click(function() {
            _this.loadStatement();
            _this.ShowLeftSubMenu('001');
        });
        //系统管理
        $("#topa_8").click(function() {
            _this.loadSetting();
            _this.ShowLeftSubMenu('001');
        });

        //退出登录操作
        $(document).on('click', '#logout', function() {
        
            _admin.logout(function(res){
                window.location.href = 'login.html';
            },function(errMsg){
                //alert('no');
            });

        });

        //起草文件培训通知
        $(document).on('click', '#trainingInform', function() {
            _this.loadTrainingInform();
        });

        //培训通知正在报名
        $(document).on('click', '#clicktraining1', function() {
             _this.loadTraining(1);
            _this.ShowLeftSubMenu('001');
        });

        //培训通知已关闭的报名
        $(document).on('click', '#clicktraining2', function() {
            _this.loadTraining(2);
            _this.ShowLeftSubMenu('001');
            $('#clicktraining1').parent('li').removeClass("Curr");
            $('#clicktraining2').parent('li').addClass("Curr");
        });


        //提交信息
        $(document).on('click', '#submit', function() {
            var formData = {
                phone: $.trim($('#phone').val()),
                title: $.trim($('#title').val()),
                importantNum: $.trim($('#importantNum').val()),
                pxDidian: $.trim($('#pxDidian').val()),
                cxDanwei: $.trim($('#cxDanwei').val()),
                zjAuthor: $.trim($('#zjAuthor').val()),
                cxDanwei: $.trim($('#cxDanwei').val()),
                content: $.trim($('#content').val()),

                pxDate: $.trim($('#pxDate').val()),
                bmEndate: $.trim($('#bmEndate').val()),


            };

            _admin.savePxtonzhi(formData, function(res) {
                alert(res);
            }, function(errMsg) {
                alert(errMsg);
            });

        });

        //左侧菜单打开子菜单
        $(document).on('click', '.FirstMenu', function() {
            _this.ShowLeftSubMenu($(this).attr("id").substring(2));
        });
        //左侧子菜单刷新右侧内容
        $(document).on('click', '#clickwork', function() {
            _this.initialize();
            _this.loadWorkTrend();
            _this.ShowLeftSubMenu('001');
        });
        //置顶状态的改变
        $(document).on('click', '#top', function() {
            _this.CancelTop($(this).parents('.wid').data('id'));

        });
    },
    /**
        初始化分页查询的date数据
    */
    initialize: function() {
        this.data.listParam.pageNum = 1;
        this.data.listParam.wtId = '';
        this.data.listParam.title = '';
        this.data.listParam.soureCrmpy = '';
    },
    //工作台
    loadOperation: function() {
        var _this = this;
        _this.loadtemplate_l(templateOperation_left);
        _this.loadtemplate_r('Operation', templateOperation_right, 'pagination_o');
    },
    //工作动态
    loadWorkTrend: function() {
        var _this = this;
        _this.loadtemplate_l(templateWorkTrend_left);
        _this.loadtemplate_r('WorkTrend', templateWorkTrend_right, 'pagination_w');
    },
    //培训通知
    loadTraining: function(state) {
        var _this = this;
        _this.loadtemplate_l(templateTraining_left);
        if (state == 1) {
            _this.loadtemplate_r('Training', templateTraining_right, 'pagination_t');
        }
        if (state == 2) {
            _this.loadtemplate_r('TrainingClose', templateTraining_right, 'pagination_t');
        }
        
    },
    //互动交流
    loadInteraction: function() {
        var _this = this;
        _this.loadtemplate_l(templateInteraction_left);
        _this.loadtemplate_r('Interaction', templateInteraction_right, 'pagination_i');
    },
    //信息发布
    loadPublish: function() {
        var _this = this;
        _this.loadtemplate_l(templatePublish_left);
        _this.loadtemplate_r('Publish', templatePublish_right, 'pagination_p');
    },
    //师资库
    loadTeacherlib: function() {
        var _this = this;
        _this.loadtemplate_l(templateTeacherlib_left);
        _this.loadtemplate_r('Teacherlib', templateTeacherlib_right, 'pagination_pl');
    },
    //报表统计
    loadStatement: function() {
        var _this = this;
        _this.loadtemplate_l(templateStatement_left);
        _this.loadtemplate_r('Statement', templateStatement_right, '');
    },
    //系统设置
    loadSetting: function() {
        var _this = this;
        _this.loadtemplate_l(templateSetting_left);
        _this.loadtemplate_r('Setting', templateSetting_right, '');
    },




    //渲染培训通知
    loadTrainingInform: function() {
        var _this = this;
        _this.loadtemplate_two(templateTrainingInform_right);
    },
    //
    loadtemplate_two: function(template) {
        var paymentHtml = "",
            _this = this,
            $pageWrap = $('.page-wrap_r');
        paymentHtml = _mm.renderHtml(template);
        $pageWrap.html(paymentHtml);

    },
    //渲染左侧菜单需要传入模版文件
    loadtemplate_l: function(template) {
        var paymentHtml = "",
            _this = this,
            $pageWrap = $('.page-wrap_l');
        //_default.getloadWorkTrend(function(res) {
        //渲染html
        paymentHtml = _mm.renderHtml(template);
        $pageWrap.html(paymentHtml);
        // }, function(errMsg) {
        //    $pageWrap.html('<p class="err-tip>' + errMsg + '</p>"');
        //});
    },
    // 加载右侧数据 第一个为模版 文件 第二个 是 分页的div类名称不需要加.
    loadtemplate_r: function(functionname, template, pagination_name) {
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.page-wrap_r'),
            functionstring;

            $pListCon.html('<div class="loading"></div>');
        //$pListCon.html('<div class="loading"></div>');
        // 请求接口
        eval("_admin.search" + functionname + "(listParam, function(res) {" +
            "_this.topFilter(res);" +
            "listHtml = _mm.renderHtml(template, {" +
            "list: res.list" +
            " });" +
            "$pListCon.html(listHtml);" +
            "_this.loadPagination({" +
            "hasPreviousPage: res.hasPreviousPage," +
            "prePage: res.prePage," +
            "hasNextPage: res.hasNextPage," +
            "nextPage: res.nextPage," +
            "pageNum: res.pageNum," +
            "pages: res.pages" +
            "},template,pagination_name,functionname);" +
            "}, function(errMsg) {" +
            "alert('查询错误！');" +
            /*"window.location.href = './error.html';" +*/
            "})");
    },
    //置顶操作
    CancelTop: function(wid) {
        var _this = this;
        _admin.selectOrState({
            wtId: wid
        }, function(res) {
            _this.loadWorkTrend();
            _this.ShowLeftSubMenu('001');
        }, function(errMsg) {

        });
    },
    // 加载分页信息
    loadPagination: function(pageInfo, template, pagination_name, functionname) {
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container: $('.' + pagination_name),
            onSelectPage: function(pageNum) {
                _this.data.listParam.pageNum = pageNum;
                _this.loadtemplate_r(functionname, template, pagination_name);
            }
        }));
    },
    //工作动态模块置顶操作筛选器 拦截所有 状态为 0 的值改变为null
    topFilter: function(data) {
        for (var i = 0, length = data.list.length; i < length; i++) {
            if (data.list[i].stateZt == 0) {
                data.list[i].stateZt = null;
            }
        }

    },


    /**
        渲染左右菜单的方法
    */
    ShowLeftSubMenu: function(MenuId) {
        var Control_FirstMenuId = "f_" + MenuId;
        var Control_FirstMenu_AId = "a_" + MenuId;
        var Control_SecondMenu_Id = "div_" + MenuId;

        //alert(Control_FirstMenuId + Control_SecondMenu_Id);

        if ($("#" + Control_SecondMenu_Id).css("display") == "none") {
            $(".SecondMenu").css("display", "none");
            $(".FirstMenu").find("span").css("background-position", "-547px -132px");

            $("#" + Control_FirstMenuId).css("border-bottom", "1px solid #b1cde2");
            $("#" + Control_FirstMenu_AId).find("span").css("background-position", "-543px -112px").show(370);
            $("#" + Control_SecondMenu_Id).show(0);

        } else {
            $("#" + Control_FirstMenuId).css("border-bottom", "1px solid #b1cde2");
            $("#" + Control_FirstMenu_AId).find("span").css("background-position", "-547px -132px").show(370);
            $("#" + Control_SecondMenu_Id).hide(0);
        }
    },

    loaderror: function() {
        var paymentHtml = "",
            _this = this,
            $pageWrap = $('.page-wrap_r');
        paymentHtml = _mm.renderHtml(error);
        $pageWrap.html(paymentHtml);
    }
    /*// 加载右侧数据 第一个为模版 文件 第二个 是 分页的div类名称不需要加.
    loadtemplate_r: function(functionname, template, pagination_name) {
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.page-wrap_r');
        //$pListCon.html('<div class="loading"></div>');
        // 请求接口
        eval("_admin.search" + functionname + "(listParam, function(res) {" +
            "_this.topFilter(res);" +
            "listHtml = _mm.renderHtml(template, {" +
            "list: res.list" +
            " });" +
            "$pListCon.html(listHtml);" +
            "_this.loadPagination({" +
            "hasPreviousPage: res.hasPreviousPage," +
            "prePage: res.prePage," +
            "hasNextPage: res.hasNextPage," +
            "nextPage: res.nextPage," +
            "pageNum: res.pageNum," +
            "pages: res.pages" +
            "},template, pagination_name," + functionname + ");" +
            "}, function(errMsg) {" +
            "_mm.errorTips(errMsg);" +
            "})");
    },*/


};
$(function() {
    page.init();
})